package com.unytech.wisdomcommunity.pager;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.unytech.wisdomcommunity.R;
import com.unytech.wisdomcommunity.bean.ComplainRepairBean;
import com.unytech.wisdomcommunity.constant.NetConstant;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by zed on 2016/11/17/0017.
 */

/**
 * "投诉报修"的pager
 */
public class ComplainRepairPager {

    private View mRootView;
    private Context mContext;
    private final BitmapUtils mBitmapUtils;
    private OkHttpClient mClient;
    private Call mCall;
    private List<ComplainRepairBean.DataBean.ResultBean> mRecordInLV;

    public ComplainRepairPager(Context context, OkHttpClient client, Call call) {
        mClient = client;
        mCall = call;
        mBitmapUtils = new BitmapUtils(context);
        mContext = context;
        mRootView = View.inflate(context, R.layout.complain_repair_pager, null);
        ListView listView = (ListView) mRootView.findViewById(R.id.pager_comlain_repair_lv);
//        listView.setEmptyView(setEmptyView);//当listView为空时   显示该View
        listView.setAdapter(mBaseAdapter);

        initData();
    }

    private void initData() {
        //发送请求
        // 设置get请求参数的方式
        Uri uri = Uri.parse(NetConstant.BASE_URL).buildUpon()
                .appendPath("msds/ws/getComplainRepair")   // 拼接路径, 路径开头不要有斜杠
                .appendQueryParameter("pageNo", "1")
                .appendQueryParameter("pageSize", "15")
                .build();

        Log.i("ComplainRepairPager", " uri - " + uri.toString());

        // 1. 封装请求
        final Request request = new Request.Builder()
                //.url(BASE_URL + "/favorites?page=0&pageNum=10")   // url后面直接拼接参数
                .url(uri.toString())
                //.addHeader("userid", "20428")   // 加请求头
                .get()
                .build();
        // 2. 发送请求
        mCall = mClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("ComplainRepairPager", "onFailure: " + e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                int code = response.code();
                Log.d("ComplainRepairPager ", "onResponse: 响应码" + code);
                String json = response.body().string();

                ComplainRepairBean complainRepairBean = new Gson().fromJson(json, ComplainRepairBean.class);
                if (complainRepairBean!=null){
                    mRecordInLV = complainRepairBean.getData().getResult();

                    mBaseAdapter.notifyDataSetChanged();//更新界面
                }
            }
        });
    }

    private  BaseAdapter mBaseAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return mRecordInLV == null ? 0 : mRecordInLV.size();
        }

        @Override
        public Object getItem(int position) {
            return mRecordInLV.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.complain_repair_item, null);
                ViewHolder viewHolder = new ViewHolder();
                viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.item_complain_record_iv);
                viewHolder.mTitleTv = (TextView) convertView.findViewById(R.id.item_complain_record_title_tv);
                viewHolder.mRespTv = (TextView) convertView.findViewById(R.id.item_complain_record_resp_tv);
                viewHolder.mDateTv = (TextView) convertView.findViewById(R.id.item_complain_record_date_tv);

                convertView.setTag(viewHolder);
            }
            ComplainRepairBean.DataBean.ResultBean singleRecord= mRecordInLV.get(position);
            ViewHolder viewHolder = (ViewHolder) convertView.getTag();
            mBitmapUtils.display(viewHolder.mImageView, singleRecord.getImage());//第二个参数是个uri
//            viewHolder.mImageView
//            viewHolder.mTitleTv.setText(singleRecord.title);标题
            if (0 == singleRecord.getRepairId() || null ==(Integer)singleRecord.getRepairId()){//"repairId"为0或者为空  就是投诉
                viewHolder.mTitleTv.setText("投诉");
            }
            viewHolder.mRespTv.setText(singleRecord.getContent());
            viewHolder.mDateTv.setText(singleRecord.getCreateTime());//显示的时间是getCreateTime或者是getUpdateTime

            return convertView;
        }
    };

    public static class ViewHolder {
        ImageView mImageView;
        TextView mTitleTv;
        TextView mRespTv;
        TextView mDateTv;
        //TextView mTimeTv;
    }

    /**
     * 用来对外界提供所维护的View,需要在newsFragment的 PagerAdapter的 instantiateItem 用
     *
     * @return mRootView
     */
    public View getView() {
        return mRootView;
    }
}
